package com.web.kdl.controller.auth;

import com.web.kdl.common.result.CommonResult;
import com.web.kdl.entity.auth.PermissionInfo;
import com.web.kdl.pojo.Vo.auth.PermissionVo;
import com.web.kdl.pojo.form.PermissionAddForm;
import com.web.kdl.pojo.form.PermissionUpdateForm;
import com.web.kdl.service.auth.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author： shulibin
 * @Date： 2025/7/30 13:49
 * @Describe：
 */
@RestController
@RequestMapping("/permissions")
public class PermissionController {
    @Autowired
    PermissionService permissionService;

    /**
     * 查询所有权限
     * @return
     */
    @PostMapping("/getAllPermissions")
    public CommonResult<List<PermissionInfo>> getAllPermissions() {
        List<PermissionInfo> result=  permissionService.getAllPermissions();
        return CommonResult.success(result);
    }

    /**
     * 根据ID获取权限详情
     * @param id 权限ID
     * @return 权限详情
     */
    @PostMapping("/getPermissionById/{id}")
    public CommonResult<PermissionInfo> getPermissionById(@PathVariable String id) {
        PermissionInfo permission = permissionService.getById(id);
        // 实现根据ID查询权限的逻辑
        return CommonResult.success(permission);
    }

    /**
     * 创建新权限
     * @param form 权限信息
     * @return 创建结果
     */
    @PostMapping("/createPermission")
    public CommonResult createPermission(@RequestBody PermissionAddForm form) {
        return permissionService.createPermission(form);
    }

    /**
     * 更新权限信息
     * @param id 权限ID
     * @param form 更新的权限信息
     * @return 更新结果
     */
    @PostMapping("/updatePermission/{id}")
    public CommonResult updatePermission(@PathVariable String id,@Valid @RequestBody PermissionUpdateForm form) {
        return permissionService.updateById(id,form);
    }

    /**
     * 删除权限
     * @param id 权限ID
     * @return 删除结果
     */
    @DeleteMapping("/deletePermission/{id}")
    public CommonResult deletePermission(@PathVariable String id) {
        return permissionService.removeById(id);
    }


    /**
     * 根据权限编码查询权限
     * @param permissionCode 权限编码
     * @return 权限信息
     */
    @PostMapping("/getPermissionByCode/{permissionCode}")
    public CommonResult<PermissionInfo> getPermissionByCode(@PathVariable String permissionCode) {
        PermissionInfo permission = permissionService.getByCode(permissionCode);
        // 实现根据编码查询权限的逻辑
        return CommonResult.success(permission);
    }

    /**
     * 获取用户拥有的所有权限
     * @param userId 用户ID
     * @return 权限列表
     */
    @GetMapping("/users/{userId}/permissions")
    public CommonResult<List<PermissionInfo>> getUserPermissions(@PathVariable String userId) {
        // 实现查询用户所有权限的逻辑（通过角色关联）
        return CommonResult.success(permissionService.getUserPermissions(userId));
    }

    /**
     * 添加批量删除权限接口
     * @param  ids
     * @return
     */
    @DeleteMapping("/deletePermissions")
    public CommonResult deletePermissions(@RequestBody List<String> ids) {
        return permissionService.removeBatchByIds(ids);
    }

}
